package com.fly.platform.common.utils

import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.BlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy
import java.util.concurrent.TimeUnit

/**
 * @author Fly
 */
object ThreadPoolUtil {
    /**
     * 线程缓冲队列
     */
    private val bqueue: BlockingQueue<Runnable> = ArrayBlockingQueue(100)

    /**
     * 核心线程数，会一直存活，即使没有任务，线程池也会维护线程的最少数量
     */
    private const val SIZE_CORE_POOL = 5

    /**
     * 线程池维护线程的最大数量
     */
    private const val SIZE_MAX_POOL = 10

    /**
     * 当前线程数超过核心线程数时，空闲线程存活时间
     */
    private const val ALIVE_TIME: Long = 2000

    private val pool= ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL, ALIVE_TIME, TimeUnit.MILLISECONDS,
            bqueue, CallerRunsPolicy())

    @JvmStatic
    fun main(args: Array<String>) {
        println(pool.poolSize)
    }

    fun getPool() : ThreadPoolExecutor {

        return pool
    }
    init {
        pool.prestartAllCoreThreads()
    }
}